package com.example.myapplication.utils

import android.content.ContentUris
import android.content.Context
import android.provider.MediaStore
import com.example.myapplication.appViewModel
import com.example.myapplication.ui.VideoFolder
import com.example.myapplication.ui.VideoItem
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.util.TreeMap

data class VideoData(
    val videoList: ArrayList<VideoItem>,
    val foldersMap: TreeMap<String, VideoFolder>
)

object VideoUtils {
    /**
     *
     * @param context
     */
    fun getVideoFolders(context: Context): VideoData {
        val foldersMap = TreeMap<String, VideoFolder>()
        val videoList = java.util.ArrayList<VideoItem>()

        val projection = arrayOf(
            MediaStore.Video.Media._ID,
            MediaStore.Video.Media.DISPLAY_NAME,
            MediaStore.Video.Media.DURATION,
            MediaStore.Video.Media.SIZE,
            MediaStore.Video.Media.DATA,
            MediaStore.Video.Media.RESOLUTION
        )
        val cursor = context.contentResolver.query(
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            projection,
            null,
            null,
            "${MediaStore.Video.Media.DATA} DESC"
        )

        cursor?.use {
            val idColumn = it.getColumnIndexOrThrow(MediaStore.Video.Media._ID)
            val pathColumn = it.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)
            while (it.moveToNext()) {
                val id = it.getLong(idColumn)
                val name =
                    it.getString(it.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME))
                val duration =
                    it.getLong(it.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION))
                val size = it.getLong(it.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE))
                val path = it.getString(pathColumn)
                val uri =
                    ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id)
                val resolution =
                    it.getString(it.getColumnIndexOrThrow(MediaStore.Video.Media.RESOLUTION))

                val videoItem = VideoItem(id.toString(), uri, name, duration, size, path, resolution)
                videoList.add(videoItem)


                val folderPath = File(path).parent ?: continue
                val folderName = File(folderPath).name

                // 统计文件夹视频数量
                if (foldersMap.containsKey(folderPath)) {
                    foldersMap[folderPath]?.count = foldersMap[folderPath]?.count?.plus(1) ?: 0
                } else {
                    // 生成首个视频的缩略图URI
                    val uri = ContentUris.withAppendedId(
                        MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id
                    )
                    foldersMap[folderPath] = VideoFolder(
                        folderPath, folderName, 1, uri
                    )
                }
                foldersMap[folderPath]?.videos?.add(videoItem)
            }
        }
        return VideoData(videoList, foldersMap)
    }

}